#include "curl_wrapper.h"
#include <iostream>

namespace curl_wrapper {
    static bool initialized = false;
    
    bool initialize() {
        if (!initialized) {
            CURLcode code = curl_global_init(CURL_GLOBAL_ALL);
            if (code != CURLE_OK) {
                std::cerr << "curl_global_init failed: " << curl_easy_strerror(code) << std::endl;
                return false;
            }
            initialized = true;
            std::cout << "CURL global initialization complete" << std::endl;
        }
        return true;
    }
    
    void cleanup() {
        if (initialized) {
            curl_global_cleanup();
            initialized = false;
            std::cout << "CURL global cleanup complete" << std::endl;
        }
    }
    
    size_t writeCallback(void* contents, size_t size, size_t nmemb, void* userp) {
        ((std::string*)userp)->append((char*)contents, size * nmemb);
        return size * nmemb;
    }
    
    std::string get(const std::string& url) {
        if (!initialize()) {
            return "";
        }
        
        CURL* curl = curl_easy_init();
        std::string response;
        
        if (curl) {
            curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
            curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
            
            CURLcode res = curl_easy_perform(curl);
            if (res != CURLE_OK) {
                std::cerr << "curl_easy_perform failed: " << curl_easy_strerror(res) << std::endl;
                response = "";
            }
            
            curl_easy_cleanup(curl);
        }
        
        return response;
    }
    
    std::string getWithParams(const std::string& baseUrl, const std::string& params) {
        if (!initialize()) {
            return "";
        }
        
        CURL* curl = curl_easy_init();
        std::string response;
        
        if (curl) {
            char* encodedParams = curl_easy_escape(curl, params.c_str(), params.length());
            std::string url = baseUrl + encodedParams;
            curl_free(encodedParams);
            
            curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
            curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
            
            CURLcode res = curl_easy_perform(curl);
            if (res != CURLE_OK) {
                std::cerr << "curl_easy_perform failed: " << curl_easy_strerror(res) << std::endl;
                response = "";
            }
            
            curl_easy_cleanup(curl);
        }
        
        return response;
    }
    
    std::string post(const std::string& url, const std::string& data) {
        if (!initialize()) {
            return "";
        }
        
        CURL* curl = curl_easy_init();
        std::string response;
        
        if (curl) {
            curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
            curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
            
            CURLcode res = curl_easy_perform(curl);
            if (res != CURLE_OK) {
                std::cerr << "curl_easy_perform failed: " << curl_easy_strerror(res) << std::endl;
                response = "";
            }
            
            curl_easy_cleanup(curl);
        }
        
        return response;
    }
    
    // Helper class for automatic CURL global resource cleanup
    class CurlCleanup {
    public:
        ~CurlCleanup() {
            cleanup();
        }
    };
    
    // Automatically cleanup CURL at program end
    static CurlCleanup cleanupHelper;
} 